package com.woniuxy.mini;

import com.woniuxy.core.service.OrderRunService;
import com.woniuxy.core.service.RunService;
import com.woniuxy.entity.AllRun;
import com.woniuxy.entity.DTO.GrabEvent;
import com.woniuxy.entity.DTO.RunAcceptDTO;
import com.woniuxy.entity.DTO.RunPostMessageDTO;
import com.woniuxy.entity.OrderRun;
import com.woniuxy.entity.ShopOrder;
import com.woniuxy.entity.VO.RunPostMessageVO;
import com.woniuxy.enums.ResponseEnum;
import com.woniuxy.util.PageResult;
import com.woniuxy.util.ResponseData;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("run-mini")
public class RunController {
    private static final Logger log = LoggerFactory.getLogger(RunController.class);
    @Autowired
    private RunService runService;
    @Autowired
    private OrderRunService orderRunService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //获取订单
    @GetMapping("/getShopOrder")
    public ResponseData getShopOrder(@RequestParam(value = "runNumber") String runNumber){
        ShopOrder shopOrder = orderRunService.getOrderByRN(runNumber);
        return ResponseData.ok(shopOrder);
    }
    //获取跑腿帖子信息
    @PostMapping("/getRunList")
    public ResponseData getRunList(@RequestBody RunPostMessageVO runPostMessageVO){
        PageResult pageResult = runService.getListAll(runPostMessageVO);
        return ResponseData.ok(pageResult);
    }
    //发布任务
    @PostMapping("/pubRun")
    public ResponseData pubRun(@RequestBody AllRun allRun){
        runService.pubRun(allRun);
        return ResponseData.ok();
    }
    //根据id获取订单信息
    @GetMapping("/getRunById")
    public ResponseData getRunById(@RequestParam("id") Integer id){
        AllRun allRun = runService.getById(id);
        return ResponseData.ok(allRun);
    }
    //完成任务
    @GetMapping("/updateState")
    public ResponseData updateState(@RequestParam("id") Integer id){
        runService.updateState(id);
        return ResponseData.ok();
    }
    //接取任务
    @GetMapping("/acceptRun")
    public ResponseData acceptRun(@RequestParam(value = "runNumber") String runNumber,
                                  @RequestParam(value = "userId") Integer userId){
        //使用redis进行扣减库存操作
        Boolean aBoolean = runService.acceptRun(runNumber, userId);
        log.info("返回值",aBoolean);
        if (aBoolean){
            GrabEvent grabEvent = new GrabEvent();
            grabEvent.setTaskId(runNumber);
            grabEvent.setUserId(userId);
            runService.sendGrabEvent(grabEvent);
            return ResponseData.ok("购买成功");
        }
        return ResponseData.fail(ResponseEnum.SYS_BUSY);
    }
    //ceshi
    @PostMapping("/create")
    public ResponseData createOrder(@RequestBody GrabEvent grabEvent){
        orderRunService.createOrder(grabEvent);
        return ResponseData.ok();
    }
    @GetMapping("/updatePay")
    public ResponseData updatePay(@RequestParam("runNumber") String runNumber){
        orderRunService.updateStateToPay(runNumber);
        return ResponseData.ok();
    }
    @PostMapping("/addOrder")
    public ResponseData addOrder(@RequestBody GrabEvent grabEvent){
        orderRunService.createOrder(grabEvent);
        return ResponseData.ok();
    }
}
